﻿using BLL.Organization;
using Common;
using Enums;
using Enums.Sys;
using Model.Organization;
using Model.Sys;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DAGG.Controllers.Organization
{
    public class EnterpriseController : BaseController
    {
        EnterpriseBLL bll = new EnterpriseBLL();
        //
        // GET: /Product/
        public ActionResult Index(string term="",int page = 1)
        {
            SysUserModel userModel = base.GetUserInfo();
            var enterId = userModel.EnterpriseId;
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere+="  ";
            else
            {
                var listWhere = string.Join(",", bll.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oe.ID  in (0))";
                else
                    strWhere += " and (oe.ID in (" + listWhere + "))";
            }

            IPagedList<EnterpriseModel> modelInfoList = bll.GetList(strWhere, term).ToPagedList(page, 10);

            ViewBag.MyPageList = modelInfoList;
            return View();
        }

        /// <summary>
        /// 新增编辑界面
        /// </summary>
        /// <param name="enterId"></param>
        /// <returns></returns>
        public ActionResult Edit(string oper, int enterId = 0)
        {
            //获取实体对象信息
            EnterpriseModel model = bll.GetModel(enterId);
            if (oper == "add") model.Code = bll.GetEnterpriseCode();//机构编号
            string strWhere = string.Empty;
            SysUserModel userModel = base.GetUserInfo();
            List<SelectListItem> BindList = new List<SelectListItem>();
            BindList.Add(new SelectListItem { Text = "无", Value = "0" });
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (oe.ID  in (0))";
                else
                    strWhere += " and (oe.ID in (" + listWhere + "))";
            }
             
            //绑定下拉框
            List<EnterpriseModel> list = bll.GetList(strWhere,"");
            bool flage = false;
            foreach (var item in list)
            {
                if (item.ID == model.SuperiorsOrganization) flage = true;

                BindList.Add(new SelectListItem { Text = item.EnterpriseName, Value = item.ID.ToString(), Selected = flage });

            }

            ViewData["BindList"] = BindList;
            return View(model);
        }
        /// <summary>
        /// 新增或编辑操作
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveData(EnterpriseModel model)
        {
            SysUserModel userModel = base.GetUserInfo();
            //获取到编辑框中的值 向表Origins_OriginsTemplet 中插入值信息
           ResultEntity entity= bll.SaveData(model, userModel);
            return Redirect("/Enterprise/Index");
        }


    }
}